package org.oos.atom.elements;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.api.client.util.Key;

public class Link implements Serializable {

	private static final long serialVersionUID = 1L;

	@Key("@href")
	protected String href;

	@Key("@rel")
	protected String rel;

	@Key("@type")
	protected String type;

	public Link() {
		super();
	}
	
	public Link(String rel, String href) {
		this.rel = rel;
		this.href = href;
	}

	public static List<Link> find(List<Link> links, String rel) {
		List<Link> results = new ArrayList<Link>();
		if (links != null) {
			for (Link link : links) {
				if (rel.equals(link.rel)) {
					results.add(link);
				}
			}
		}
		return results;
	}

	public String getHref() {
		return href;
	}

	public URL getURL() {
		try {
			return new URL(getHref());
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getRel() {
		return rel;
	}

	public String getType() {
		return type;
	}

	@Override
	public String toString() {
		return href;
	}

}
